from app.model.cs_admins import CsAdmins
from libs.db import DB


def get_next_job_num(shop_id):
    admin = CsAdmins.objects.filter(shop_id=shop_id).order_by('-job_num').values('job_num').first()
    if admin is None:
        job_num = 1001
    else:
        job_num = admin.get('job_num') + 1
    return job_num


def get_admin(admin_id, fields=None):
    if fields is None:
        fields = ['id', 'shop_id', 'job_num', 'username', 'status', 'last_login_at', 'last_logout_at', 'created']
    admin = {}
    try:
        admin = CsAdmins.objects.filter(id=admin_id).values(*fields).get()
    except Exception as e:
        print('exception:', e.args)
        pass
    return admin


def get_admin_by_id(cursor, admin_id, fields=None):
    if fields is None:
        fields = ['id', 'shop_id', 'job_num', 'username', 'status', 'last_login_at', 'last_logout_at', 'created']
    dh = DB('cs_admins', conn=cursor)
    return dh.one(fields, {'id': admin_id})
